package com.utils.wechat;

import com.base.Constants;
import com.bean.LogintokenContent;
import com.bean.LogintokenSource;
import com.bean.WebsiteUserInfo;
import com.bean.wechat.ResultInfo;
import com.bean.wechat.WechatContent;
import com.bean.wechat.WechatWebsite;
import com.mapper.mysql.UserInfoMapper;
import com.mapper.repository.mysql.LogintokenContentRepository;
import com.mapper.repository.mysql.WechatContentRepository;
import com.mapper.repository.mysql.WechatWebsiteRepository;
import com.service.LogintokenService;
import com.utils.BaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;

@Service
public class WechatStrategyLogintoken implements WechatStrategy  {

    //解决绑定问题
    //map是输入
    /**
     *
     * @param resultInfo  返回的结果
     * @param content     请求的内容
     */

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private WechatWebsiteRepository wechatWebsiteRepository;

    @Autowired
    private LogintokenService logintokenService;

    @Autowired
    private WechatContentRepository wechatContentRepository;

    @Autowired
    private BaseUtils baseUtils;

    @Autowired
    private LogintokenContentRepository logintokenContentRepository;

    @Override
    public void returnMsg(ResultInfo resultInfo,String content) {
        //判断当前用户是否存在，是否有数据，是否只有一个数据
        //从绑定的表中获取用户信息，然后根据用户信息查询，并返回结果{大于1的结果， 1个的链接  没有任何内容的结果}
        //判断是否绑定了用户
        String wechatid = resultInfo.getToUserName();
        WechatWebsite wechatWebsite = wechatWebsiteRepository.findByWechatid(wechatid);
        if(wechatWebsite==null){
            resultInfo.setReturnMethod("text");
            resultInfo.setContent("尚未绑定用户信息，请输入：绑定用户名:密码 ，进行绑定");
        }else{
            String request ="";
            if(content.endsWith("的信息")){
                request = content.substring(0,(content.length()-"的信息".length()));
            }else if(content.endsWith("的密码")){
                request = content.substring(0,(content.length()-"的密码".length()));
            }

            LogintokenSource logintokenSource = new LogintokenSource();
            logintokenSource.setUserid(wechatWebsite.getWebsiteuserid());
            logintokenSource.setName(request);
            List<LogintokenSource> list = logintokenService.listLogintokenSource(logintokenSource);

            if(list.size()==0){
                resultInfo.setReturnMethod("text");
                resultInfo.setContent("未查询到'"+request+"'的相关信息");
            }else if(list.size()==1){
                if(content.endsWith("的信息")){
                    String id =UUID.randomUUID().toString();
                    WechatContent wechatContent = new WechatContent() ;
                    wechatContent.setId(id);
                    wechatContent.setContent(content);
                    wechatContent.setUserid(resultInfo.getToUserName());
                    wechatContent.setCreatetime(LocalDateTime.now());
                    wechatContent.setEndtime(LocalDateTime.now().plusMinutes(10l));//10分钟后过期
                    wechatContent.setType(Constants.WechatLogintoken.getValue());//设置类型
                    wechatContent.setOther(list.get(0).getId());
                    wechatContentRepository.save(wechatContent);

                    resultInfo.setReturnMethod("news");
                    resultInfo.setTitle("点击查看"+content);
                    resultInfo.setDescription("查询到了"+request+"的相关信息，请查看");
                    resultInfo.setPicUrl(baseUtils.getBaseConfigByName("wechat_search_png"));
                    //resultInfo.setUrl(baseUtils.getWechatUrl()+"?wid="+id);
                    resultInfo.setUrl(baseUtils.getWechatUrl()+"wechat/wechat_logintoken.html?wid="+id);
                }else if(content.endsWith("的密码")){

                    LogintokenSource logintokenSource1 = list.get(0);
                    LogintokenContent logintokenContent = logintokenContentRepository.findLogintokenContentBySid(logintokenSource1.getId());
                    resultInfo.setReturnMethod("text");
                    resultInfo.setContent(request+"'的密码为："+logintokenContent.getPassword());
                }



            }else if(list.size()>1){
                resultInfo.setReturnMethod("text");
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append("搜索到如下内容:\n");
                for(LogintokenSource more : list){
                    stringBuffer.append(more.getName()+"\n");
                }
                stringBuffer.append("请回复以上内容的信息");
                resultInfo.setReturnMethod("text");
                resultInfo.setContent(stringBuffer.toString());
            }
        }

    }

}
